﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ToolsCXml;
using System.IO;
using System.Web;

namespace ShowScreen
{
    public class CGetNetData
    {
        public CGetNetData()
        {
            _Filelist = new CFileList();
            _Imgdownload = new ImgDownLoad();
        }
        public bool DownLoadData()
        {
            string str = _Filelist.GetData();
            Msg = str.Replace("&lt;", "<")
                .Replace("&gt;", ">")
                .Replace("&amp;", "&")
                .Replace("&quot;", "\"")
              ;
            return true;
        }
        public bool DownLoadImgData()
        {
            Msg = _Imgdownload.GetData(_Imgdownload.URL);
            return true;
        }
        public System.Drawing.Image DownLoadImage(string url)
        {
            return _Imgdownload.GetImage(url);
        }
        private CFileList _Filelist;
        private ImgDownLoad _Imgdownload;
        public string Msg { get; set; }

    }
    public class CFileList
    {
        public CFileList()
        {
            url = @"https://gitee.com/ljhbeer/ScreenData/blob/master/filelist.data";
        }


        public string GetData()
        {
            string str = CItem.web.GetOKUrl(url);
            BETags tags = new BETags("[<textarea-/textarea>][>-<]");
            string str2 = tags.Match(str);
            List<string> list = str2.Split(new string[] { "&#x000A;" }, StringSplitOptions.RemoveEmptyEntries).ToList();
            str2 = string.Join("\r\n", list.ToArray());

            return str2;
        }
        private string url;
    }
    public class ImgDownLoad
    {
        public ImgDownLoad()
        {
            URL = @"https://gitee.com/ljhbeer/ScreenData/blob/master/Screen/HxTest-1.docx/0001.jpg";
        }

        public string GetData(string url)
        {
            string str = CItem.web.GetOKUrl(url);
            BETags tags = new BETags("[file_content-</div>][src='-']");
            string str2 = tags.Match(str);
            //List<string> list = str2.Split(new string[] { "&#x000A;" }, StringSplitOptions.RemoveEmptyEntries).ToList();
            //str2 = string.Join("\r\n", list.ToArray());
            str2 = str2.Replace("&#x000A;", "\r\n");
            Base64ToImage(str2, "E:\\img\\test.jpg");
            return str2;
        }

        public System.Drawing.Image GetImage(string url)
        {
            string str = CItem.web.GetOKUrl(url);
            BETags tags = new BETags("[file_content-</div>][src='-']");
            str = tags.Match(str);
            str = str.Replace("&#x000A;", "\r\n");
            return  Base64ToImage(str);
        }

        private System.Drawing.Image Base64ToImage(string base64)
        {
            base64 = base64.Replace("\r\n", "").Replace("data:image/jpeg;base64,", "");
            string dummyData = base64.Trim().Replace("%", "").Replace(",", "").Replace(" ", "+");
            if (dummyData.Length % 4 > 0)
            {
                dummyData = dummyData.PadRight(dummyData.Length + 4 - dummyData.Length % 4, '=');
            }
            byte[] arr2 = Convert.FromBase64String(dummyData);
            MemoryStream ms2 = new MemoryStream(arr2);
            return new System.Drawing.Bitmap(ms2);
        }
        public void Base64ToImage(string base64,string filePath)
        {
            //将Base64String转为图片并保存
            base64 = base64.Replace("\r\n", "").Replace("data:image/jpeg;base64,","");
            string dummyData = base64.Trim().Replace("%", "").Replace(",", "").Replace(" ", "+");
            if (dummyData.Length % 4 > 0)
            {
                dummyData = dummyData.PadRight(dummyData.Length + 4 - dummyData.Length % 4, '=');
            }
            byte[] arr2 = Convert.FromBase64String(dummyData);
            using (MemoryStream ms2 = new MemoryStream(arr2))
            {
                System.Drawing.Bitmap bmp2 = new System.Drawing.Bitmap(ms2);
                ////只有把当前的图像复制一份，然后把旧的Dispose掉，那个文件就不被锁住了，
                ////这样就可以放心覆盖原始文件，否则GDI+一般性错误(A generic error occurred in GDI+)
                //System.Drawing.Bitmap bmpNew = new System.Drawing.Bitmap(bmp2);
                //bmp2.Dispose();
                //bmp2 = null;
                bmp2.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg);
                //bmp2.Save(filePath + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
                //bmp2.Save(filePath + ".bmp", System.Drawing.Imaging.ImageFormat.Bmp);
                //bmp2.Save(filePath + ".gif", System.Drawing.Imaging.ImageFormat.Gif);
                //bmp2.Save(filePath + ".png", System.Drawing.Imaging.ImageFormat.Png);
                bmp2.Dispose();
            }
        }

        public string ImageToBase64()
        {
            string UserPhoto = "";
            //保存目录
            string dir = "/upload/user/head";
            //站点文件目录
            string fileDir = HttpContext.Current.Server.MapPath("~" + dir);
            //文件名称
            string fileName = "headdemo" + DateTime.Now.ToString("yyyyMMddHHmmssff");
            //保存文件所在站点位置
            string filePath = Path.Combine(fileDir, fileName);

            if (!System.IO.Directory.Exists(fileDir))
                System.IO.Directory.CreateDirectory(fileDir);

            //读图片转为Base64String
            System.Drawing.Bitmap bmp1 = new System.Drawing.Bitmap(Path.Combine(fileDir, "default.jpg"));
            using (MemoryStream ms1 = new MemoryStream())
            {
                bmp1.Save(ms1, System.Drawing.Imaging.ImageFormat.Jpeg);
                byte[] arr1 = new byte[ms1.Length];
                ms1.Position = 0;
                ms1.Read(arr1, 0, (int)ms1.Length);
                ms1.Close();
                UserPhoto = Convert.ToBase64String(arr1);
            }
            return UserPhoto;
        }
        public string URL { get; set; }

    }
}
